using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public class tbtinhthanhDAL
    {
        private SqlConnection cn = null;
        public tbtinhthanhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbtinhthanhDAL(tbtinhthanhDAL thach)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbtinhthanh tinhthanh)
        {
            SqlCommand cm = new SqlCommand("tinhthanh_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tentinh", tinhthanh.Tentinh);
            cm.ExecuteNonQuery();
        }
        public void Update(tbtinhthanh tinhthanh)
        {
            SqlCommand cm = new SqlCommand("tinhthanh_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matinh", tinhthanh.Matinh);
            cm.Parameters.AddWithValue("@tentinh",tinhthanh.Tentinh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tinhthanh_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matinh", iD);
            cm.ExecuteNonQuery();
        }
       public tbtinhthanhcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tinhthanh_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbtinhthanhcollection tinhthanhCollection = new tbtinhthanhcollection();
            while (dr.Read())
            {
                tbtinhthanh tinhthanh = new tbtinhthanh();
                tinhthanh.Matinh = int.Parse(dr.GetValue(0).ToString());
                tinhthanh.Tentinh = dr.GetValue(1).ToString();
                tinhthanhCollection.Add(tinhthanh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return tinhthanhCollection;
        }
    }
}
